<?php
declare(strict_types=1);
namespace App\System\Controller;

use Haozing\FastCore\Abstract\AbstractController;
use Haozing\FastCore\Constants\ErrorCode;
use Haozing\FastCore\Exception\CommonException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\System\Logic\HeiMenuLogic;

#[Controller]
class HeiMenuController extends AbstractController
{
    #[Inject]
    protected HeiMenuLogic $heiMenuLogic;

    /**
     * 新增
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/add',methods: 'post')]
    public function add(): array
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'parent_id' => 'required',
                'level' => 'required',
                'name' => 'required',
                'code' => 'required',
                'icon' => 'required',
                'route' => 'required',
                'component' => 'required',
                'redirect' => 'required',
                'is_hidden' => 'required',
                'type' => 'required',
                'status' => 'required',
                'sort' => 'required',
                'created_by' => 'required',
                'updated_by' => 'required',
                'created_at' => 'required',
                'remark' => 'required',

            ],
            [
                'parent_id.required' => '父ID不能为空',
                'level.required' => '组级集合不能为空',
                'name.required' => '菜单名称不能为空',
                'code.required' => '菜单标识代码不能为空',
                'icon.required' => '菜单图标不能为空',
                'route.required' => '路由地址不能为空',
                'component.required' => '组件路径不能为空',
                'redirect.required' => '跳转地址不能为空',
                'is_hidden.required' => '是否隐藏 (1是 2否)不能为空',
                'type.required' => '菜单类型, (M菜单 B按钮 L链接 I iframe)不能为空',
                'status.required' => '状态 (1正常 2停用)不能为空',
                'sort.required' => '排序不能为空',
                'created_by.required' => '创建者不能为空',
                'updated_by.required' => '更新者不能为空',
                'created_at.required' => '创建时间不能为空',
                'remark.required' => '备注不能为空',

            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiMenuLogic->add($params);

        return $data;
    }
    /**
     * 删除
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/delete',methods: 'post')]
    public function delete(): array
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiMenuLogic->delete($params);

        return $data;
    }

    /**
     * 更新
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/update',methods: 'post')]
    public function update(): array
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiMenuLogic->update($params);

        return [];
    }

    /**
     * 列表查询
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/list',methods: 'post')]
    public function list(): array
    {
        // 验证参数
        $params = $this->request->all();
        // 执行业务逻辑
        $data = $this->heiMenuLogic->list($params);
        return $data;

    }
    /**
     * 列表查询select
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/select',methods: 'post')]
    public function select(): array
    {
        // 验证参数
        $params = $this->request->all();
        // 执行业务逻辑
        $data = $this->heiMenuLogic->select($params);
        return $data;

    }
    /**
     * 详情
     * @return array
     */
    #[RequestMapping(path: '/system/heiMenu/detail',methods: 'get')]
    public function detail(): array
    {
        //验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiMenuLogic->detail($params);
        return $data;
    }

}